function User_CallBack(response)
{ 
	if (response.error != null)
	{ 
		document.getElementById('d').innerHTML=(response.error); 
		return; 
	}
	
	var text = response.value;
	document.getElementById('u').innerHTML=text;
	setTimeout("burble.getUsers(User_CallBack)",30000);
}

function Msg_CallBack(response)
{ 
	if (response.error != null)
	{ 
		document.getElementById('d').innerHTML=(response.error); 
		return; 
	}
	
	var text = decrypt(response.value);
	var objDiv = document.getElementById('d');
	objDiv.innerHTML+=text;
	objDiv.scrollTop = objDiv.scrollHeight;
	setTimeout("burble.getMsg(Msg_CallBack)",1500);
}

function SendMsg(text)
{
	var name = document.getElementById('txtUserName').value;
	var text = document.getElementById('txtMsg').value;
	var newtext = "[" + name + " " + getTime() + "] " + text + "<br>";
	newtext = encrypt(newtext);
	burble.sendMsg(newtext);
	document.getElementById('txtMsg').value='';
}
function encrypt(plaintext)
{
	var keyval = getKeyVal();
	var result = "";
	for (var i=0; i<plaintext.length; i++)
	{
		var x = plaintext.charCodeAt(i);
		x = (x + keyval)%256;
		result += String.fromCharCode(x);
	}
	
	return result;
}

function decrypt(ciphertext)
{
	var keyval = getKeyVal();
	var result = "";
	for (var i=0; i<ciphertext.length; i++)
	{
		var x = ciphertext.charCodeAt(i);
		if (x <= 0)
		{
			x = 256-x;					}
		x = (x - keyval)%256;
		result += String.fromCharCode(x);
	}
	
	return result;
}	

function getKeyVal()
{
	var keyval = 0;
	var key = document.getElementById('txtKey').value;
	for (var i=0; i<key.length; i++)
	{
		keyval += key.charCodeAt(i);
	}
	keyval = keyval%256;
	return keyval;
}	

function getTime()
{
	var date = new Date();
	var s = pad0(date.getHours(),2) + 
		":" + pad0(date.getMinutes(),2) + 
		":" + pad0(date.getSeconds(),2);
	return s;
}
function pad0(string, newlength) {
	var pad = "";
	var len = newlength-String(string).length;
	var i;
	for (i = 0; i<len; i++) {
		pad += "0";
	}
	return pad+string;
}

